import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/home',
        name: 'home',
        component: () => import('../views/home/index'),
        meta: {
            keepAlive: true,//此组件需要被缓存
            isFootShow:true,
        }
    },
    {
        path: '/home/brandlist',
        name: 'brandlist',
        component: () => import('../views/home/brandlist/index'),
    },
    {
        path: '/home/city',
        name: 'city',
        component: () => import('../views/home/city/index'),
    },
    {
        path: '/home/brandDetail',
        name: 'brandDetail',
        component: () => import('../views/home/brandDetail/index'),
    },
    {
        path: '/home/newGoods',
        name: 'newGoods',
        component: () => import('../views/home/newGoods/index'),
    },
    {
        path: '/cart',
        name: 'cart',
        component: () => import('../views/cart/index'),
        meta: {
            isFootShow:true,
        }
    },
    {
        path: '/cart/order',
        name: 'order',
        component: () => import('../views/cart/order/index'),
    },
    {
        path: '/category',
        name: 'category',
        component: () => import('../views/category/index'),
        meta: {
            keepAlive: true,//此组件需要被缓存
            isFootShow:true,
        }
    },
    {
        path: '/my',
        name: 'my',
        component: () => import('../views/my/index'),
        meta: {
            keepAlive: true,//此组件需要被缓存
            isFootShow:true,
        }
    },
    {
        path: '/my/collect',
        name: 'collect',
        component: () => import('../views/my/collect/index'),
    },
    {
        path: '/my/feedback',
        name: 'feedback',
        component: () => import('../views/my/feedback/index'),
    },
    {
        path: '/my/address',
        name: 'address',
        component: () => import('../views/my/address/index'),
    },
    {
        path: '/my/address/addAddress',
        name: 'addAddress',
        component: () => import('../views/my/address/addAddress/index'),
    },
    {
        path: '/topic',
        name: 'topic',
        component: () => import('../views/topic/index'),
        meta: {
            keepAlive: true,//此组件需要被缓存
            isFootShow:true,
        }
    },
    {
        path: '/topic/topicDetail',
        name: 'topicDetail',
        component: () => import('../views/topic/topicDetail/index'),
    },
    {
        path: '/homeCategory',
        name: 'homeCategory',
        component: () => import('../views/homeCategory/index'),
    },
    {
        path: '/pages/goods',
        name: 'goods',
        component: () => import('../views/pages/goods/index'),
    },
    {
        path: '/pages/search',
        name: 'search',
        component: () => import('../views/pages/search/index'),
    },
]
// 点击同一个商品编程式路由报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push  = function (location) {
    return originalPush.call(this, location).catch(err => err)
}
const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes,
    // 让滚动条滑到顶部
    scrollBehavior (to, from, savedPosition) {
        // 只要不是下方导航，就将滚动条置顶
        if (to.path != '/home' && to.path != '/topic' && to.path != '/category') {
            return {x:0,y:0}
        }
    }
})
// 导航守卫
router.beforeEach((to, from, next) => {
    // 退出商品页面前清空商品详情
    if (from.path == '/pages/goods') {
        router.app.$store.commit('clrGoods');
        next()
    }
    next()
})

export default router
